﻿using System;
using System.Linq;
using System.Web.Mvc;
using Yonkly.Filters;
using Yonkly.Models;
using Yonkly.Utility;

namespace Yonkly.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class TimelineController : BaseController
    {
        protected override void HandleUnknownAction(string actionName)
        {
            RedirectToAction("View");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page">page number to display</param>
        [CompressFilter]
        public ActionResult View(int? page)
        {
            //throw new Exception("This is an error");
            return View(("View"), GetTimelineViewData(page));
        }

        private MessagesViewData GetTimelineViewData(int? page)
        {
            page = page ?? 0;

            var viewData = new MessagesViewData(GetBaseViewData());

            //get user messages
            using (var dc = new YonklyDataContext())
            {
                var query = (from message in dc.ViewMessages
                             select new MessageItem(viewData, message) { CreatedOn = message.CreatedOn }
                            ).OrderByDescending(m => m.CreatedOn);

                viewData.Messages = query.ToPagedList((int)page, Globals.PageSize);
            }
            return viewData;
        }
    }
}
